﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenNI;
using BTKinect.DataType;

namespace BTKinect.DataIO
{
    class KinectSelectDesiredData
    {
        #region CONSTRUCTORS
        public KinectSelectDesiredData(KinectData k)
        {
            kd = k;
            length = kd.DataAndTime.Count;
        }
        public KinectSelectDesiredData(KinectData k, int roundingNumberOfDecimals)
        {
            kd = k;
            length = kd.DataAndTime.Count;
            _roundingNumberOfDecimals = roundingNumberOfDecimals;
        }

        #endregion

        #region MEMBERS
        private int length;
        private KinectData kd;
        private int _roundingNumberOfDecimals = 0;
        #endregion

        #region METHODS
        public List<KinectPoint3D[]> GetJointsData(SkeletonJoint joint)
        {
            List<KinectPoint3D[]> lstPoints = new List<KinectPoint3D[]>(length);
            KinectPoint3D[] pnt3DArray = new KinectPoint3D[1];

            //put desired data in List
            foreach (var item in kd.Data)
            {
                pnt3DArray = new KinectPoint3D[1];

                pnt3DArray[0] = item[joint].Round(_roundingNumberOfDecimals);
                lstPoints.Add(pnt3DArray); //save points
            }
            return lstPoints;
        }
        public List<KinectPoint3D[]> GetJointsData(SkeletonJoint joint1, SkeletonJoint joint2)
        {
            List<KinectPoint3D[]> lstPoints = new List<KinectPoint3D[]>(length);
            KinectPoint3D[] pnt3DArray = new KinectPoint3D[2];

            //put desired data in List
            foreach (var item in kd.Data)
            {
                pnt3DArray = new KinectPoint3D[2];

                pnt3DArray[0] = item[joint1].Round(_roundingNumberOfDecimals);
                pnt3DArray[1] = item[joint2].Round(_roundingNumberOfDecimals);

                lstPoints.Add(pnt3DArray); //save points
            }
            return lstPoints;
        }
        public List<KinectPoint3D[]> GetJointsData(SkeletonJoint joint1, SkeletonJoint centerJoint, SkeletonJoint joint2)
        {
            List<KinectPoint3D[]> lstPoints = new List<KinectPoint3D[]>(length);
            KinectPoint3D[] pnt3DArray = null;

            //put desired data in List
            foreach (var item in kd.Data)
            {
                pnt3DArray = new KinectPoint3D[3]; //essential or al other data in lstPoints reference to the same data

                pnt3DArray[0] = item[joint1].Round(_roundingNumberOfDecimals);
                pnt3DArray[1] = item[centerJoint].Round(_roundingNumberOfDecimals);
                pnt3DArray[2] = item[joint2].Round(_roundingNumberOfDecimals);

                lstPoints.Add(pnt3DArray); 
            }

            
            //put selected data in right List
            switch (centerJoint)
            {
                case SkeletonJoint.LeftElbow:
                    kd.ElbowLeft = lstPoints;
                    break;
                case SkeletonJoint.LeftKnee:
                    kd.KneeLeft = lstPoints;
                    break;
                case SkeletonJoint.LeftShoulder:
                    kd.ShoulderLeft = lstPoints;
                    break;
                case SkeletonJoint.RightElbow:
                    kd.ElbowRight = lstPoints;
                    break;
                case SkeletonJoint.RightKnee:
                    kd.KneeRight = lstPoints;
                    break;
                case SkeletonJoint.RightShoulder:
                    kd.ShoulderRight = lstPoints;
                    break;
                default:
                    break;
            }

            return lstPoints;
        }
        public List<KinectPoint3D[]> GetJointsData(List<SkeletonJoint> listJoint)
        {
            List<KinectPoint3D[]> lstPoints = new List<KinectPoint3D[]>(length);
            KinectPoint3D[] pnt3DArray = null;

            //put desired data in List
            foreach (var item in kd.Data)
            {
                pnt3DArray = new KinectPoint3D[3]; //essential or al other data in lstPoints reference to the same data


                pnt3DArray[0] = item[listJoint[0]].Round(_roundingNumberOfDecimals);
                pnt3DArray[1] = item[listJoint[1]].Round(_roundingNumberOfDecimals);
                pnt3DArray[2] = item[listJoint[2]].Round(_roundingNumberOfDecimals);

                lstPoints.Add(pnt3DArray); 
            }

            //put selected data in right List
            switch (listJoint[1])
            {
                case SkeletonJoint.LeftElbow:
                    kd.ElbowLeft = lstPoints;
                    break;
                case SkeletonJoint.LeftKnee:
                    kd.KneeLeft = lstPoints;
                    break;
                case SkeletonJoint.LeftShoulder:
                    kd.ShoulderLeft = lstPoints;
                    break;
                case SkeletonJoint.RightElbow:
                    kd.ElbowRight = lstPoints;
                    break;
                case SkeletonJoint.RightKnee:
                    kd.KneeRight = lstPoints;
                    break;
                case SkeletonJoint.RightShoulder:
                    kd.ShoulderRight = lstPoints;
                    break;
                default:
                    break;
            }


            return lstPoints;
        }
        public KinectData GetKinectData
        {
            get { return kd; }
        }
        public void Initialize()
        { //empty 
        }
        #endregion
    }
}
